﻿using Common;
using PJSIP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace SIPSDk.Pjsip
{
    public class PjsipProvider : SIPProvider
    {
        private Telephony pjsipTelephony;
        private SipManager sipManager;

        public override void InitialSIP()
        {
            sipManager = new SipManager(Thread.CurrentThread);
            sipManager.AccountStateChange += SipManager_AccountStateChange;
            sipManager.CallStateChange += SipManager_CallStateChange;
            sipManager.IncomingCall += SipManager_IncomingCall;
        }

        private void SipManager_IncomingCall(object sender, Data.IncomingCallEventArgs e)
        {
        }

        private void SipManager_CallStateChange(object sender, Data.CallStateEventArgs e)
        {

        }

        private void SipManager_AccountStateChange(object sender, Data.AccountStateEventArgs e)
        {
            Logger.Info("注册状态改变！");
        }

        public override void Answer()
        {
           pjsipTelephony.AcceptIncomingCall();
        }

        public override void Call(string dialedNumber)
        {
            pjsipTelephony.MakeCall(dialedNumber);
        }

        public override void CallCancel()
        {

        }

        public override void CallHold()
        {

        }

        public override void HangUpCall()
        {
            pjsipTelephony.HangUp();
        }

        public override void OffSip()
        {
            pjsipTelephony.Dispose();
        }

        public override void RegisterAccount()
        {
            Logger.Info("开始注册！");
            sipManager.addAccount(UserSIPConfig.UserSIPId, UserSIPConfig.UserSIPPassword, UserSIPConfig.UserRegistServerIP, UserSIPConfig.DefaultAvailableUDPPort);
        }
        
        public override void Reject()
        {
            pjsipTelephony.DeclineIncomingCall();
        }

        public override void UnRegisterAccount()
        {

        }

        public override void SendDTMFMessage(string msg)
        {
            pjsipTelephony.SendDTMFMessage(msg);
        }

        public override bool CheckDevice()
        {
            return pjsipTelephony.CheckDevice();
        }

        #region 私有方法


        #endregion

        #region 私有方法

        #endregion

        #region 私有事件

        private void PjsipTelephony_IncomingCall(object sender, CallArgs.ComingCallArgs e)
        {
            OnComingCallReceived(sender, e);
        }

        private void PjsipTelephony_IncomingCallEnded(object sender, CallArgs.ComingCallArgs e)
        {
            //TODO:对方取消打电话
        }

        private void PjsipTelephony_StateChanged(object sender, EventArgs e)
        {

        }

        private void PjsipTelephony_DTMFMessageReceived(object sender, CallArgs.DTMFMessageReceiveArgs e)
        {
            OnDTMFMessageComming(sender, new CallArgs.DTMFMessageReceiveArgs(e.DigitMsg));
        }

        #endregion
    }
}
